﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using System.Web;
using Antu.WorkFlowFactory;
using System.Data;
using System.Configuration;

namespace WebSys.UI.OfficeFlow
{
    public class WorkFlowAfterSubmitSkin : MPBasicTemplate
    {
        FlowFactoryProvider _FlowFactoryProvider = (FlowFactoryProvider)ProviderService.ProviderServiceMange.GetProvider("FlowFactoryProvider");
        string wiid, ctlId, iid, wid, userid, step;
        protected string UserId
        {
            get { if (base.Page.Session["UserId"] != null) { return base.Page.Session["UserId"].ToString(); } else { return string.Empty; } }
            set { base.Page.Session["UserId"] = value; }
        }
        protected override void OnLoad(EventArgs e)
        {
            LoginHelper.CheckLogin();
            InitControl();
            base.OnLoad(e);
            if (Submit())
            {
                base.Page.Response.Write("<script>window.parent.reloadandclose();</script>");
            }
        }

        private void InitControl()
        {
            wiid = base.Page.Request["wiid"];
            ctlId = HttpContext.Current.Server.UrlDecode(base.Page.Request["CTLID"]);
            iid = base.Page.Request["iid"];
            wid = _FlowFactoryProvider.GetWidByIID(iid);
            userid = UserId;
            step = _FlowFactoryProvider.GetStepName(ctlId,wid);
            bool canopen = _FlowFactoryProvider.CanOpen(userid, iid, wiid, step);
            bool isopen = _FlowFactoryProvider.IsOpen(iid, wiid, step);
            //if (!canopen || isopen)
            //{
            //    Antu.Utilities.UtilitiesPage.AlertDialogClear("该业务已被处理或无权限处理！", "window.parent.closeDialog();");
            //    return;
            //}
            string msg = "";
            //if (false == _FlowFactoryProvider.TaskOpenCheck(iid, wiid, step, userid, out msg))
            if (false == _FlowFactoryProvider.TaskOpenCheck(iid, wiid, step, userid, out msg))
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("alert('" + msg + "！');", "window.parent.closeDialog();");
                return;
            }
        }


        private bool Submit()
        {
            string[] flowIds = base.Page.Request.Params.GetValues("flows");
            if (BeforeSubmit(iid, wid, wiid, ctlId))
            {
                //兼容多流向提交
                FlowUserStruct[] fus = new FlowUserStruct[flowIds.Length];
                string msg = "";
                for (int temp = 0; temp < flowIds.Length; temp++)
                {
                    string[] users = base.Page.Request.Params.GetValues("User_" + flowIds[temp]);
                    fus[temp].FlowName = flowIds[temp];
                    fus[temp].Users = users;
                }
                bool result = _FlowFactoryProvider.SubmitInstance(long.Parse(iid), long.Parse(wiid), fus, ref msg);
                if (result == false)
                {
                    Antu.Utilities.UtilitiesPage.AlertDialog("提交没有成功，原因是：" + msg + "", "history.back();");
                    return false;
                }

                AfterSubmit(iid, wid, wiid, ctlId);
            }
            else
            {
                return false;
            }
            return true;
        }

        //归档之前
        private bool BeforeSubmit(string iid, string wid, string wiid, string ctlId)
        {
            _FlowFactoryProvider.FlowToGuiDang(iid, wid, wiid);

            return true;
        }

        
        private bool AfterSubmit(string iid, string wid, string wiid, string ctlId)
        {
            return true;
        }
    }
}


